﻿using PagedList;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Model.Origins;
using Model.Sys;
using BLL.Origins;
using BLL.Organization;
using Enums.Sys;
using Enums;
namespace DAGG.Controllers.CodeManage
{
    public class ApplyCodeController : BaseController
    {
      
        ApplyCodeBLL bll = new ApplyCodeBLL();
        EnterpriseBLL bll0 = new EnterpriseBLL();
        //
        // GET: /ApplyCode/
        public ActionResult Index(int page=1)
        {
            SysUserModel user = base.GetUserInfo();
            string strWhere = string.Empty;
           
               // var listWhere = string.Join(",", bll0.GetALLButNoSelf(user.EnterpriseId).Select(obj => obj.ID));
              //  if (string.IsNullOrEmpty(listWhere))
                    strWhere += " and (oa.EnterPriseId  in ("+user.EnterpriseId+"))";
              //  else
                 //   strWhere += " and (oa.EnterPriseId in (" + listWhere + "))";
          
            //根据登录用户机构得到该机构下的所有用户信息
                    IPagedList<ApplyCodeModel> ApplyList = bll.Getlist(strWhere).ToPagedList(page, 10);
            ViewBag.MyPageList = ApplyList;
            return View();
          
        }
        public ActionResult Edit(int id=0)
        {
            ApplyCodeModel model = new ApplyCodeModel();
            if(id!=0) model = bll.GetModel(id);
            return View(model);
        }
        public ActionResult SaveData(ApplyCodeModel model)
        {
            SysUserModel user = base.GetUserInfo();
            if (model.ID == 0) {
                model.EnterPriseId = user.EnterpriseId;
                model.CreateLoginID = user.ID;
                model.CreateLoginName = user.RealName;
                model.CreateDate = DateTime.Now;
                bll.AddApply(model);

            }
            else
            {
                model.ChangeLoginID = user.ID;
                model.ChangeLoginName = user.RealName;
                model.ChangeDate = DateTime.Now;
                bll.UpdateApply(model);
            }
            return Redirect("/ApplyCode/Index");
        }
        public ActionResult Apply(int Id)
        {
            bll.UpdateStaus(Id);
            return Redirect("/ApplyCode/Check");
        }
        public ActionResult Check(int page=1)//审核所有子机构申请
        {
            SysUserModel user = base.GetUserInfo();
            string strWhere = string.Empty;
            if (EnumManagement.GetEnumValue(SysRoleEnum.SuperManageRole) == user.RoleId)//管理员角色 查看全部
                strWhere += "  ";
            else
            {
                var listWhere = string.Join(",", bll0.GetALLButNoSelf(user.EnterpriseId).Select(obj => obj.ID));
                if (string.IsNullOrEmpty(listWhere))
                    strWhere += " and (oa.EnterPriseId  in (0))";
                else
                    strWhere += " and (oa.EnterPriseId in (" + listWhere + "))";
            }

            //根据登录用户机构得到该机构下的所有用户信息
            IPagedList<ApplyCodeModel> ApplyList = bll.Getlist(strWhere).ToPagedList(page, 10);
            ViewBag.MyPageList = ApplyList;
            return View(); 
        }
	}
}